package com.gomcarter.frameworks.redis.factory;

import com.gomcarter.frameworks.redis.RedisProperties;
import com.gomcarter.frameworks.redis.tool.RedisProxy;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author gomcarter on 2019-11-19 11:07:19
 */
public class RedisFactory implements FactoryBean<RedisProxy>, InitializingBean {
    private RedisProperties redisProperties;
    private RedisProxy proxy = new RedisProxy();

    @Override
    public RedisProxy getObject() throws Exception {
        return proxy;
    }

    @Override
    public Class<? extends RedisProxy> getObjectType() {
        return (this.proxy != null ? this.proxy.getClass() : RedisProxy.class);
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        RedisConnectionBuilder.of(proxy, redisProperties);
    }

    public RedisProperties getRedisProperties() {
        return redisProperties;
    }

    public RedisFactory setRedisProperties(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
        return this;
    }
}
